package com.feng.gulimall.member.controller;

import com.feng.common.exception.BizCodeEnume;
import com.feng.common.utils.PageUtils;
import com.feng.common.utils.R;
import com.feng.common.vo.GiteeUser;
import com.feng.common.vo.SocialUser;
import com.feng.gulimall.member.entity.MemberEntity;
import com.feng.gulimall.member.exception.PhoneException;
import com.feng.gulimall.member.exception.UsernameException;
import com.feng.gulimall.member.feign.CouponFeignService;
import com.feng.gulimall.member.service.MemberService;
import com.feng.gulimall.member.vo.MemberLoginVo;
import com.feng.gulimall.member.vo.MemberRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 会员
 *
 * @author fengge
 * @email sunlightcs@gmail.com
 * @date 2022-09-25 22:54:33
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private CouponFeignService couponFeignService;
    @PostMapping("/oauth2/login")
    public R oauthlogin(@RequestBody GiteeUser giteeUser){
      MemberEntity memberEntity=  memberService.login(giteeUser);
      return R.ok().put("data",memberEntity);
    }

   @RequestMapping("/coupons")
    public R test(){
       MemberEntity memberEntity = new MemberEntity();
       memberEntity.setNickname("张三");
       R membercoupons = couponFeignService.membercoupons();
       return R.ok().put("member",memberEntity).put("coupons",membercoupons.get("coupons"));
   }
   @PostMapping("/regist")
   public R register(@RequestBody MemberRegisterVo vo){
       try {
           memberService.register(vo);
       } catch (PhoneException e) {
           return R.error(BizCodeEnume.PHONE_EXIST__EXCEPTION.getCode(),BizCodeEnume.PHONE_EXIST__EXCEPTION.getMsg());
       } catch (UsernameException e) {
           return R.error(BizCodeEnume.USER_EXIST_EXCEPTION.getCode(),BizCodeEnume.USER_EXIST_EXCEPTION.getMsg());
       }
       return R.ok();
   }
   @PostMapping("/login")
   public R login(@RequestBody MemberLoginVo memberLoginVo){
      MemberEntity memberEntity= memberService.login(memberLoginVo);
      if(memberEntity==null){
        return R.error(BizCodeEnume.LOGININACCT_PASSWORD_INVAILD_EXCEPTION.getCode(),BizCodeEnume.LOGININACCT_PASSWORD_INVAILD_EXCEPTION.getMsg());
      }
      else{
          return R.ok().put("data",memberEntity);
      }

   }

    /**
     * 列表
     */
    @RequestMapping("/list")
   // @RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
